package com.guaoran.agricultural.big.data;

import com.guaoran.newsdata.api.hystrix.NewsDataCoreServiceFallback;
import com.guaoran.newsdata.api.service.NewsDataCoreService;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @Author gucheng
 * @Description 农业大数据网站启动类
 * 2019-07-18 10:58
 */
@SpringBootApplication
@EnableEurekaClient
// 开启 feign 方式调用
@EnableFeignClients(clients = {NewsDataCoreService.class})
@EnableHystrixDashboard
public class AgriculturalBigDataApplication {
    public static void main(String[] args) {
        SpringApplication.run(AgriculturalBigDataApplication.class,args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * {@link NewsDataCoreService} 使用 feign 方式调用，失败后的熔断，
     * @Desc 因为 {@link NewsDataCoreService} 是通过 pom 文件引入的，不通过该方式会找不到 Bean
     * @return
     */
    @Bean
    public NewsDataCoreServiceFallback newsDataCoreServiceFallback(){
        return new NewsDataCoreServiceFallback();
    }

    /**
     * hystrix 服务熔断监控
     * http://127.0.0.1:8080/hystrix/
     * 监控Hystrix Stream: http://127.0.0.1:8080/hystrix.stream
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);  //系统启动时加载顺序
        registrationBean.addUrlMappings("/hystrix.stream");//路径
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

